package com.icode.web.controller.house;

import com.icode.core.dto.about.FaqDTO;
import com.icode.core.dto.order.PageDTO;
import com.icode.core.model.about.Faq;
import com.icode.core.model.order.Order;
import com.icode.service.about.FaqService;
import com.icode.service.house.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.swing.plaf.OptionPaneUI;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: song
 * Date: 15-12-9
 * Time: 下午4:49
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping(value = "/faq")
public class FaqController {
    @Autowired
    private FaqService faqService;
    @Autowired
    private HouseService houseService;

    @RequestMapping(value = "front_faq", method = RequestMethod.GET)
    public String OpenFaqPage() {
        String s = "12";
        System.out.println(s);
        return "/front/aboutFaq";
    }

    @RequestMapping(value = "front_list", method = RequestMethod.POST)
    public String findFaqs(String question, Model model) {
        List<FaqDTO> faqDTOList = faqService.findFaqs(question);
        model.addAttribute("faqDTOList", faqDTOList);
        return "front/aboutFaq";
    }

    @RequestMapping(value = "back_add", method = RequestMethod.GET)
    public ModelAndView faqUpdate() {
        return new ModelAndView("/back/about/faqUpdate", "faqDTO", new FaqDTO());
    }

    @RequestMapping(value = "back_add_faq", method = RequestMethod.POST)
    public String addFaq(FaqDTO faqDTO) {
        faqService.addFaqsList(faqDTO);
        return "redirect:/faq/back_findAll/1";
    }

    @RequestMapping(value = "back_update/{guid}", method = RequestMethod.GET)
    public ModelAndView updateFaq(@PathVariable String guid, Model model) {
        FaqDTO faqDTO = faqService.findFaqByGuid(guid);
        model.addAttribute("faqDTO", faqDTO);
        return new ModelAndView("/back/about/faqUpdate", "faqDTO", faqDTO);
    }

    @RequestMapping(value = "back_findAll/{currentPage}", method = RequestMethod.GET)
    public String findFaqList(Model model, @PathVariable String currentPage) {
        String totalPage = houseService.findTotalPage(Faq.class).toString();
        currentPage = PageDTO.isPage(currentPage, totalPage);
        List<FaqDTO> allFaqDTOs = faqService.findAllFaqsByPage(currentPage);
        model.addAttribute("allFaqDTOs", allFaqDTOs);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        return "back/about/faqOverView";
    }

    @RequestMapping(value = "back_delete/{faqGuid}", method = RequestMethod.GET)
    public String deleteFaq(@PathVariable String faqGuid) {
        faqService.deleteFaq(faqGuid);
        return "redirect:/faq/back_findAll/1";
    }
}
